package com.oig.sys.workflow.msg.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.oig.common.constants.CommonConstants;
import com.oig.common.util.JsonUtil;
import com.oig.sys.msg.api.NoticeMsgService;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.msg.dto.NoticeMsgDto;
import com.oig.sys.msg.dto.NoticeMsgReceiverDto;
import com.oig.sys.msg.vo.NoticeMsgVo;
import com.oig.sys.workflow.dto.AuditCallMsgDto;
import com.oig.sys.workflow.msg.IDepNoticeMsgService;
import com.oig.sys.workflow.vo.AutoOperationVo;
import com.oig.sys.workflow.vo.AutoRuleVo;
import com.oig.sys.workflow.vo.CronJobVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class DepNoticeMsgServiceImpl implements IDepNoticeMsgService {

    private final NoticeMsgService noticeMsgService;

    @Override
    public void addAutoRuleNoticeMsg(AutoRuleVo autoRuleVo, AutoOperationVo autoOperation, AuditCallMsgDto auditCallMsgDto, String updateFiled) {
        NoticeMsgDto noticeMsgDto = new NoticeMsgDto();
        noticeMsgDto.setTitle(autoOperation.getNotifyTitle());
        Map<String, Object> objectMap = JsonUtil.stringToCollect(auditCallMsgDto.getObjJson());
        if (StrUtil.isNotBlank(updateFiled)) {
            Map<String, Object> stringObjectMap = JsonUtil.stringToCollect(auditCallMsgDto.getOldValue());
            Object oldValue = stringObjectMap.get(updateFiled);
            String oldValueStr = "" ;
            if (ObjectUtil.isNotEmpty(oldValue)) {
                // 去掉小数点
                oldValueStr = oldValue.toString();
                oldValueStr = StrUtil.subBefore(oldValueStr, ".", true);
            }
            objectMap.put("old-" + updateFiled, oldValueStr);
        }
        //autoOperation.getNotifyContent() 使用{xx} 占位
        //MessageFormat.format() 替换占位
        noticeMsgDto.setContent(StrUtil.format(autoOperation.getNotifyContent(), objectMap));      //考虑是否用占位符
        noticeMsgDto.setMsgType(autoOperation.getNotifyType());
        noticeMsgDto.setPushChannelId(autoOperation.getPushChannelId());
        noticeMsgDto.setPushChannelName(autoOperation.getPushChannelName());
        noticeMsgDto.setTenantId(autoRuleVo.getTenantId());
        noticeMsgDto.setTenantName(autoRuleVo.getTenantName());
        noticeMsgDto.setReceiverType(autoOperation.getReceiverType());
        if (MsgEnums.ReceiverType.USER.getCode().equals(autoOperation.getReceiverType())) {
            NoticeMsgReceiverDto noticeMsgReceiverDto = new NoticeMsgReceiverDto();
            noticeMsgReceiverDto.setReceiverName(autoOperation.getReceiverName());
            noticeMsgReceiverDto.setReceiverId(autoOperation.getReceiverId());
            List<NoticeMsgReceiverDto> list = CollUtil.toList(noticeMsgReceiverDto);
            noticeMsgDto.setNoticeMsgReceiverList(list);
        } else {
            noticeMsgDto.setReceiverId(autoOperation.getReceiverId());
            noticeMsgDto.setReceiverName(autoOperation.getReceiverName());
        }
        log.debug("add notifyMsg : {}",noticeMsgDto);
        noticeMsgService.addSysNoticeMsg(noticeMsgDto) ;
    }


    @Override
    public NoticeMsgVo addJobNoticeMsg(CronJobVo cronJob, String executeMsg) {
        //预警消息，定时任务失败通知
        NoticeMsgDto noticeMsgDto = new NoticeMsgDto();
        noticeMsgDto.setTenantId(cronJob.getTenantId());
        noticeMsgDto.setTenantName(cronJob.getTenantName());
        noticeMsgDto.setTitle(cronJob.getNotifyTitle());
        noticeMsgDto.setContent(cronJob.getNotifyContent()+ CommonConstants.LEFT_MIDDLE_BOAST+executeMsg+CommonConstants.RIGHT_MIDDLE_BOAST);
        noticeMsgDto.setMsgType(cronJob.getNotifyType());
        noticeMsgDto.setPushChannelId(cronJob.getPushChannelId());
        noticeMsgDto.setPushChannelName(cronJob.getPushChannelName());
        noticeMsgDto.setReceiverType(cronJob.getReceiverType());
        noticeMsgDto.setReceiverId(cronJob.getReceiverId());
        noticeMsgDto.setReceiverName(cronJob.getReceiverName());
        NoticeMsgVo msgVo = null;
        try{
            msgVo = noticeMsgService.addSysNoticeMsg(noticeMsgDto) ;
        } catch (Exception e){
            log.error("add notice msg error", e);
        }
        return msgVo;
    }
}
